#!/bin/bash

#requires not make

BOUT_TOP=../../..
error=

for i in BoutReal # double float int char size_t
do
    # Search for "(... const $i...)", but exclude lines with "template"
    # because sometimes "const BoutReal" is needed in template specialisations
    grep -E "\([^\)]*const[^\(,:<]*$i[^,\):\*>]*\&"  -r --include=*xx $BOUT_TOP | grep -v template &&
        error=yes &&
        echo "const $i should be passed by value"
done
#for emacs regexp: \([,\(]\)[[:space:]]*const[[:space:]]+\([A-Za-z]+\)[[:space:]]+\([A-Za-z0-9]+\)[[:space:]]*\([,\)]\) → \1\2 \3\4)
grep [\(,][[:space:]]*const[[:space:]][[:alnum:]]*[[:space:]][[:alnum:]]*[[:space:]]*[,\)] -r --include=*hxx $BOUT_TOP -n &&
    error=yes &&
    echo "Declaration of functions shouldn't declare pass-by-value as const"


paths="src include examples tests"


# Check that exceptions are caught by reference
for path in $paths
do
    catch_errors=$(grep catch[^\(]*\([^\(]*Bout $BOUT_TOP/$path --include=*xx -r|grep -v '&')
    ex=$?
    if test $ex -eq 0
    then
        error=yes
        echo "Found catch by value - not by reference:"
        echo "$catch_errors"
    fi
done


# Return
if test $error
then
    echo "-> Errors listed above"
    exit 1
else
    echo "No Errors detected"
    exit 0
fi
